package com.mac.macjetpackdemo.ui

import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.res.Configuration
import android.os.Bundle
import android.os.IBinder
import android.os.Process
import android.util.Log
import androidx.annotation.MainThread
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.mac.macjetpackdemo.IMyAidlInterface
import com.mac.macjetpackdemo.R
import com.mac.macjetpackdemo.other.MeetSmallApp
import com.mac.macjetpackdemo.other.SmallApp
import com.mac.macjetpackdemo.other.SmallAppWrapper
import com.mac.macjetpackdemo.service.MyService
import com.mac.macjetpackdemo.singClick
import com.mac.macjetpackdemo.ui.search.SearchResultActivity
import com.mac.macjetpackdemo.ui.type.TypeActivity
import com.mac.macjetpackdemo.util.*
import com.mac.macjetpackdemo.view.FloatView
import com.stoa.floatlib.StoaFloat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    var cycleObserver: CycleObserver? = null
    private val TAG = "MainActivity"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setFullScreen()
        cycleObserver = CycleObserver()
        cycleObserver?.let {
            lifecycle.addObserver(it)
        }
        mSearch.setOnClickListener {
//            testMMKVPut()
            FloatView().show()
            StoaFloat.show("screen")
            val keyword = mMainEt.text.toString().trim()
            if (keyword.isEmpty()) {
                toast("搜索内容不能为空")
            } else {
                val intent = Intent(this, SearchResultActivity::class.java)
                intent.putExtra(SearchResultActivity.LOAD_TYPE_LEY, 0)
                intent.putExtra(SearchResultActivity.KEYWORD_KEY, keyword)
                startActivity(intent)
            }
        }

        mBtn.setOnClickListener {
//            testMMKVGet()
//            StoaFloat.hide("video")
            Log.e(
                "MyService",
                " 0 " + getProcessName(this) + " : " + SingleTest.getInstance()
                    .hashCode() + " ' " + GlideUtil.hashCode()
            )

            val intent = Intent(this, TypeActivity::class.java)
            startActivity(intent)

            val i = Intent(this, MyService::class.java)
            bindService(i, object : ServiceConnection {
                override fun onServiceDisconnected(name: ComponentName?) {

                }

                override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                    val asInterface = IMyAidlInterface.Stub.asInterface(service)
                    asInterface.start(Intent())
                }
            }, Context.BIND_AUTO_CREATE)
        }

        mTest.singClick {
            val smallAppWrapper: SmallApp = SmallAppWrapper()
            val meetSmallApp: SmallApp = MeetSmallApp(smallAppWrapper)
            meetSmallApp.onMessage("mTest")

        }
    }

    private fun testMMKVPut() {
        SpUtils.putInt("testInt", 123)
        SpUtils.putBoolean("testBoolean", true)
        val byteArray = ByteArray(2)
        byteArray[0] = 127
        byteArray[1] = -128
        SpUtils.putBytes("testBytes", byteArray)
        SpUtils.putString("testString", "aaaaaqqqqqqqqqqwwwwwwww")
        val set = mutableSetOf<String>()
        set.add("a")
        set.add("a")
        set.add("b")
        set.add("b")
        set.add("c")
        SpUtils.putStringSet("testStringSet", set)
    }

    private fun testMMKVGet() {
        SpUtils.getBytes("testBytes").forEach {
            Log.d(TAG, "testMMKVGet: $it")
        }
        Log.d(
            TAG,
            "testMMKVGet: ${SpUtils.getInt("testInt")} : ${SpUtils.getBoolean("testBoolean")} : \n " +
                    "${SpUtils.getBytes("testBytes")} : ${SpUtils.getString("testString")} : ${SpUtils.getStringSet(
                        "testStringSet"
                    )}"
        )
    }

    fun getProcessName(cxt: Context): String? {
        val pid = Process.myPid()
        val am =
            cxt.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
                ?: return ""
        val runningApps =
            am.runningAppProcesses ?: return ""
        for (procInfo in runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName
            }
        }
        return ""
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        cycleObserver?.log("onConfigurationChanged $newConfig")
    }

    override fun onDestroy() {
        super.onDestroy()
        cycleObserver?.let {
            lifecycle.removeObserver(it)
        }
    }

}
